<?php
namespace frontend\widgets;

use frontend\models\Page;
use yii\bootstrap\Widget;

class LeftSideMenu extends Widget
{
    public $currentNode;

    private $neighborsList;
    private $parentNode;
    private $allParentsNode;
    
    public function init()
    {
        parent::init();

        //$this->node = Page::findOne(['id' => (int)$this->nodeID]);
        $this->parentNode = $this->currentNode->getParent()
            ->select('title')
            ->asArray()
            ->one();
        $this->neighborsList = $this->currentNode->parent->getChildren()
            ->andWhere(['is_draft' => Page::PUBLISHED])
            ->andWhere(['is_hidden' => Page::VISIBLE])
            ->select('id, title, alias')
            ->asArray()
            ->all();
        $this->allParentsNode = $this->currentNode->getParents()
            ->select('id, alias')
            ->asArray()
            ->all();
    }

    public function run()
    {   
        //echo '<h3 id="sub-menu-heading">' . $this->parentNode['title'] . '</h3>';
        echo '<ul class="sub-menu">';
        foreach ($this->neighborsList as $neighbor) 
        {
            if($neighbor['id'] == $this->currentNode->id)
            {
                echo '<li><h4 id="sub-menu-active">' . $neighbor['title'] . '</h4></li>';
            }
            else
            {
                echo '<li><h4><a href="' . $this->generateURL($neighbor['alias']) . '">' . $neighbor['title'] . '</a></h4></li>';
            }
            
        }
        echo '</ul>';
    }

    
    private function generateURL($alias)
    {
        $path = '/';

        foreach ($this->allParentsNode as $key => $parentItem) 
        {
            if($key != 0)
            {
                $path .= $parentItem['alias'] . '/';
            }
            
        }
        $path .= $alias;

        return $path;
    }
    


}